﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using SZHL.UI.AppCode;
using FastReflectionLib;
using SZHL.Data;
using Newtonsoft.Json;
using System.Data;

namespace SZHL.UI.API
{
    public class NOTEManage : IWsService
    {
        public void ProcessRequest(HttpContext context, ref Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            MethodInfo methodInfo = typeof(NOTEManage).GetMethod(msg.Action.ToUpper());
            NOTEManage model = new NOTEManage();
            methodInfo.FastInvoke(model, new object[] { context, msg, P1, P2, UserInfo });
        }
        /// <summary>
        /// 添加记事本
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void ADDNOTE(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_NOTE note = JsonConvert.DeserializeObject<ZH_NOTE>(P1);
            if (note.NoteContent.Trim() == "")
            {
                msg.ErrorMsg = "记事本内容不能为空";
                return;
            }
            if (note.ID == 0)
            {
                note.CRDate = DateTime.Now;
                note.CRUser = UserInfo.User.UserName;
                note.UPDDate = DateTime.Now;
                note.UPUser = UserInfo.User.UserName;
                new ZH_NOTEB().Insert(note);
            }
            else
            {

                note.UPDDate = DateTime.Now;
                note.UPUser = UserInfo.User.UserName;
                new ZH_NOTEB().Update(note);
            }
            msg.Result = note;
        }
        /// <summary>
        /// 获取记事本列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETUSERNOTELIST_PAGE(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = string.Format(" CRUser='{0}' ", UserInfo.User.UserName);
            if (P1 != "")
            {
                strWhere += string.Format(" And NoteTitle like  '%{0}%'", P1);
            }
            if (P2 != "")
            {
                strWhere += string.Format(" And TagName='{0}'", P2);
            }
            int page = 0;
            int.TryParse(context.Request["p"] ?? "1", out page);
            int total = 0;
            DataTable dt = new ZH_XTGGB().GetDataPager("ZH_NOTE", "*", 8, page, " CRDate desc", strWhere, ref total);
            msg.Result = dt;
            msg.Result1 = total;
        }
        /// <summary>
        /// 删除记事本
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">ID</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void DELNOTEBYID(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {
                if (!new ZH_NOTEB().Delete(d => d.ID.ToString() == P1))
                {

                    msg.ErrorMsg = "删除失败";
                }
            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }
        /// <summary>
        /// 获取记事本信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETNOTEMODEL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            msg.Result = new ZH_NOTEB().GetEntity(d => d.ID.ToString() == P1);
        }

    }
}